package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.Alias;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工相关接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @ApiOperation("员工登录接口")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());

        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @ApiOperation("员工退出登录接口")
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }


    @ApiOperation("员工修改密码接口")
    @PutMapping("/editPassword")
    public Result editPassword(@RequestBody PasswordEditDTO passwordEdit, @RequestHeader String token) {
        log.info("PasswordEditDTO：{}", passwordEdit);


        Integer l = (Integer) JwtUtil.parseJWT(jwtProperties.getAdminSecretKey(), token).get(JwtClaimsConstant.EMP_ID);

        log.info("修改密码的员工id为:{}", l);

        passwordEdit.setEmpId(Long.valueOf(l));


        return employeeService.editPassword(passwordEdit);
    }


    @ApiOperation("根据id查询员工信息的业务接口")
    @GetMapping("/{id}")
    public Result<Employee> getEmployeeById(@PathVariable Integer id) {
        log.info("员工id：{}", id);
        Employee employee = employeeService.getEmployeeById(id);


        return Result.success(employee);

    }


    @ApiOperation("分页查询员工信息的业务接口")
    @GetMapping("/page")
    public Result<PageResult> getEmployeeByPage(EmployeePageQueryDTO dto) {

        return employeeService.getEmployeeByPage(dto);

    }


    @ApiOperation("增加员工的业务接口")
    @PostMapping()
    public Result<String> addEmployee(@RequestBody EmployeeDTO dto) {

        return employeeService.addEmployee(dto);
    }


    @ApiOperation("修改员工信息的业务接口")
    @PutMapping
    public Result updateEmployeeById(@RequestBody EmployeeDTO dto) {
        // 拷贝对象属性
        Employee  employee = new Employee();
        BeanUtils.copyProperties(dto,employee);

        //设置遗漏属性
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(BaseContext.getCurrentId());

        BaseContext.removeCurrentId();

        Result result = employeeService.updateEmployeeById(employee);

        return result;
    }

    @ApiOperation("启用、禁用员工账号的业务接口")
    @PostMapping("/status/{status}")
    public Result setEmployeeStatus(@PathVariable Integer status, @RequestParam Long id) {
        Employee employee = Employee.builder().status(status).id(id).build();

        Result result = employeeService.updateEmployeeById(employee);

        return result;
    }

}
